﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using KPIS.GERP.Models;
using KPIS.GERP.Models.HRM;
using KPIS.GERP.Models.SYS;
using KPIS.GERP.Models.SYS.ViewModel;
using KPIS.GERP.OMVC.Classes;
using KPIS.GERP.OMVC.EmployeeService;
using KPIS.GERP.OMVC.SysApprovalService;
using KPIS.GERP.OMVC.UserService;
using System.Data.Entity;

namespace KPIS.GERP.OMVC.Controllers
{
    public class ApprovePathController : Controller
    { 
        private GERPContext context = new GERPContext(true);
        private int empId {get { return new SsMng().EmpSeqI; }}
         
        public ActionResult Index(int appPrcId, int? empIdTest = null)
        {
            ViewBag.appPrcId = appPrcId;
            ViewBag.EmpIdTest = empIdTest;
            return View();
        }
        
        public ActionResult Create(int appPrcId)
        {
            ViewBag.appPrcId = appPrcId;

            ApprovePath model = new ApprovePath();
            model.AppPrcId = appPrcId;
            model.IsForNotice = false;
            model.StepNo = 1;
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(ApprovePath model, int appPrcId)
        {
            ViewBag.appPrcId = appPrcId;
            model.RecordStatus = "U";
            model = context.ApprovePaths.Add(model);
            context.SaveChanges();
            return RedirectToAction("Index", new {  model.AppPrcId});
        }
        
        public ActionResult Update(int id, int appPrcId)
        {
            ViewBag.AppPrcId = appPrcId;
            return View(context.ApprovePaths.FirstOrDefault(o=>o.AppPathId == id));
        }

        [HttpPost]
        public ActionResult Update(ApprovePath model)
        {
            ValidateModel(model);
            if(!ModelState.IsValid){
                return View(model);
            } 
            context.ApprovePaths.Attach(model);
            context.Entry(model).State = EntityState.Modified;
            context.SaveChanges();
            return RedirectToAction("Detail", new {id = model.AppPathId, appPrcId = model.AppPrcId});
        }
        
        public ActionResult Detail(int id, int appPrcId, int? addTo = null)
        {
            ViewBag.appPrcId = appPrcId;
            ViewBag.addTo = addTo;
            return View(context.ApprovePaths.FirstOrDefault(o=>o.AppPathId == id));
        }
        
        public ActionResult Delete(int id, int appPrcId)
        {
            ViewBag.appPrcId = appPrcId;
            var appPath = context.ApprovePaths.First(o => o.AppPathId == id);
            context.ApprovePaths.Attach(appPath);
            context.Entry(appPath).State = EntityState.Modified;
            appPath.RecordStatus = "D";
            context.SaveChanges();
            return RedirectToAction("Index", new {appPrcId});
        }

        [AllowAnonymous]
        public JsonResult GetExecPositionEmployee(int id)
        {   
            var employees = context.Employees.Include(o=>o.Position.ExecutivePosition).Include(o=>o.MasNamePrefix).Where(o=>o.RecordStatus != "D").ToList();
             
            var allEmployeeInExecPosId = from e in employees 
                    where e.Position.ExecPosId == id 
                    select e == null ? "" : (e.Position != null ? "[" + e.Position.PosNo + " - " + e.Position.Name + "] " : "") + 
                                            (e.MasNamePrefix != null ? e.MasNamePrefix.Name : "") + 
                                            e.FirstName + " " + e.LastName + " " + 
                                            (e.Position != null && e.Position.ExecutivePosition != null ? e.Position.ExecutivePosition.Name : "" );



            var result1 = allEmployeeInExecPosId.ToArray();
            var result2 = allEmployeeInExecPosId.Count();
            string result = string.Join("<br/>", result1); 
            return Json(new{Name = result, Count = result2}, JsonRequestBehavior.AllowGet);
        }

        public void ValidateModel(ApprovePath model)
        {
            //if(string.IsNullOrWhiteSpace(model.StepNo))
            //{
            //    ModelState.AddModelError("Name", "กรุณาใส่ชื่อ");
            //}
        }
    }
}
